﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MyService.ServiceHandler
{
    public class IniFileHelper
    {
        // INI文件路径
        private string iniFilePath = string.Empty;

        // 构造函数，初始化INI文件路径
        public IniFileHelper(string iniFilePath)
        {
            this.iniFilePath = iniFilePath;
        }

        // 写入INI文件
        [DllImport("kernel32.dll")]
        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        // 从INI文件读取值
        [DllImport("kernel32.dll")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        // 读取指定Section下的指定Key的值
        public string ReadConfig(string section, string key)
        {
            StringBuilder temp = new StringBuilder(1024);
            GetPrivateProfileString(section, key, "", temp, 1024, iniFilePath);
            return temp.ToString();
        }

        // 写入指定的Section下，写入键值对
        public void WriteConfig(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, iniFilePath);
        }

    }
}
